//
//  MSFilterMultipleViewController.m
//  SocietySale
//
//  Created by Mike on 16/11/15.
//  Copyright © 2016年 Mike. All rights reserved.
//

#import "MSFilterMultipleViewController.h"
#import "MSFilterMultipleTableVC.h"

@interface MSFilterMultipleViewController ()
@property UIView * backgroundView;
//筛选列表中选中的数据
@property (nonatomic, strong) NSMutableDictionary* params;
@property (nonatomic, strong) NSMutableArray    *mediumStateArray;    //状态
//弹出的筛选列表的视图容器
@property UIView* contentView;
@property (nonatomic, assign) NSInteger lastIndex;
@property (nonatomic, strong) UIButton *confirmBtn;
@property (nonatomic, strong) MSFilterMultipleTableVC *tableVC;
@end

@implementation MSFilterMultipleViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    //灰色背景
    self.backgroundView=[[UIView alloc]init];
    self.backgroundView.frame = self.view.bounds;
    self.backgroundView.backgroundColor=[UIColor colorWithWhite:0.f alpha:0.3f];
    self.backgroundView.clipsToBounds = YES;
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
    tapGesture.delegate = (id)self;
    [self.backgroundView addGestureRecognizer:tapGesture];
    
    self.tableVC = [[MSFilterMultipleTableVC alloc] initWithStyle:UITableViewStyleGrouped];
    self.tableVC.view.frame = CGRectMake(0, SCREEN_HEIGHT-240, SCREEN_WIDTH, 240);
    self.tableVC.enableRefresh = NO;
    self.tableVC.enableRequest = NO;
    self.tableVC.delegate = (id)self;
    self.tableVC.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.backgroundView addSubview:self.tableVC.view];
    
    _lastIndex = 0;
    if (_showConfirmBtn) {
        UIView *btnBgView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-280, SCREENWIDTH, 40)];
        btnBgView.backgroundColor = kNormalGray;
        [self.backgroundView addSubview:btnBgView];
        
        UIButton *cancellBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        cancellBtn.tag = 0;
        cancellBtn.frame = CGRectMake(10, 0, 40, 40);
        [cancellBtn setTitle:@"取消" forState:UIControlStateNormal];
        [cancellBtn setTitleColor:kLightBlackColor forState:UIControlStateNormal];
        cancellBtn.titleLabel.font = HeitiFont(15);
        [cancellBtn addTarget:self action:@selector(confirmAction:) forControlEvents:UIControlEventTouchUpInside];
        [btnBgView addSubview:cancellBtn];
        
        self.confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.confirmBtn.tag = 1;
        self.confirmBtn.frame = CGRectMake(SCREENWIDTH-50, 0, 40, 40);
        [self.confirmBtn setTitle:@"完成" forState:UIControlStateNormal];
        [self.confirmBtn setTitleColor:kBlueColor forState:UIControlStateNormal];
        self.confirmBtn.titleLabel.font = HeitiFont(15);
        [self.confirmBtn addTarget:self action:@selector(confirmAction:) forControlEvents:UIControlEventTouchUpInside];
        [btnBgView addSubview:self.confirmBtn];
    }
    
    self.mediumStateArray = [[NSMutableArray alloc] initWithCapacity:0];
    for (int i = 0; i< self.stateArray.count; i++){
        [self.mediumStateArray addObject:@"NO"];
    }
}

- (void)showInView:(UIView *)superView
{
    if (!self.backgroundView.superview) {
//        [_backgroundView addSubview:self.view];
        UIWindow *window = [UIApplication sharedApplication].windows[0];
        [window addSubview:self.backgroundView];
    }
    for (int i = 0; i< self.mediumStateArray.count; i++){
        NSString *stateStr = [self.mediumStateArray[i] isEqualToString:@"YES"] ? @"YES":@"NO";
        [self.stateArray replaceObjectAtIndex:i withObject:stateStr];
    }
    self.tableVC.stateArray = self.mediumStateArray;
    self.tableVC.dataArray = [self.dataArray mutableCopy];
    float tableViewHeight = MIN(self.tableVC.tableView.contentSize.height, 240);
    self.tableVC.view.frame = CGRectMake(0, SCREEN_HEIGHT-tableViewHeight, SCREEN_WIDTH, tableViewHeight);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (_isMultipleSelect) {
        MSFilterMultipleCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:0]];
        cell.tagImage.hidden = [self.stateArray[indexPath.row] isEqualToString:@"YES"];
        NSString *stateStr = [self.stateArray[indexPath.row] isEqualToString:@"NO"] ? @"YES":@"NO";
        [self.stateArray replaceObjectAtIndex:indexPath.row withObject:stateStr];        
    }
    else{
        if (_lastIndex != indexPath.row) {
            MSFilterMultipleCell *lastCell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:_lastIndex inSection:0]];
            lastCell.tagImage.hidden = YES;
            [self.stateArray replaceObjectAtIndex:_lastIndex withObject:@"NO"];
        }
        
        MSFilterMultipleCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.tagImage.hidden = [self.stateArray[indexPath.row] isEqualToString:@"YES"];
        NSString *stateStr = [self.stateArray[indexPath.row] isEqualToString:@"NO"] ? @"YES":@"NO";
        [self.stateArray replaceObjectAtIndex:indexPath.row withObject:stateStr];
        
        _lastIndex = indexPath.row;
        
        [self callBackData];
        [self dismiss];
    }
}

- (void)confirmAction:(UIButton*)btn
{
    if (btn.tag==1) {
        [self callBackData];
    }
    [self dismiss];
}

- (void)callBackData{
    if ([self.delegate respondsToSelector:@selector(selectedFilter)]) {
        [self.delegate selectedFilter];
    }
    for (int i = 0; i< self.stateArray.count; i++){
        NSString *stateStr = [self.stateArray[i] isEqualToString:@"YES"] ? @"YES":@"NO";
        [self.mediumStateArray replaceObjectAtIndex:i withObject:stateStr];
    }
}

- (void)dismiss
{
    [self.backgroundView removeFromSuperview];
    [self.view removeFromSuperview];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    CGPoint touchPoint = [touch locationInView:self.backgroundView];
    return !CGRectContainsPoint(self.tableVC.view.frame, touchPoint);
}

@end
